var t = require("../@babel/runtime/helpers/interopRequireDefault")(require("../@babel/runtime/helpers/typeof")), e = require("jquery");

module.exports = {
    toQueryPair: function(t, e) {
        return void 0 === e ? t : t + "=" + encodeURIComponent(null === e ? "" : String(e));
    },
    getUrl: function(n, o, i) {
        n = n.replace(/\//gi, ".");
        var a = getApp().getConfig().api + "&r=" + n;
        return o && ("object" == (0, t.default)(o) ? a += "&" + e.param(o) : "string" == typeof o && (a += "&" + o)), 
        a;
    },
    json: function(t, n, o, i, a, r) {
        var s = getApp(), c = s.getCache("userinfo_openid"), u = s.getCache("usermid"), f = s.getCache("authkey");
        (n = n || {}).comefrom = "wxapp", n.openid = "sns_wa_" + c, u && (n.mid = u.mid, 
        n.merchid = n.merchid || u.merchid);
        var d = this;
        i && d.loading(), n && (n.authkey = f || "");
        var l = {
            url: (a ? this.getUrl(t) : this.getUrl(t, n)) + "&timestamp=" + +new Date(),
            method: a ? "POST" : "GET",
            header: {
                "Content-type": a ? "application/x-www-form-urlencoded" : "application/json",
                Cookie: "PHPSESSID=" + c
            }
        };
        r || delete l.header.Cookie, a && (l.data = e.param(n)), o && (l.success = function(t) {
            if (i && d.hideLoading(), "request:ok" == t.errMsg && "function" == typeof o) {
                if (s.setCache("authkey", t.data.authkey || ""), void 0 !== t.data.sysset) {
                    if (1 == t.data.sysset.isclose) return void wx.redirectTo({
                        url: "/pages/message/auth/index?close=1&text=" + t.data.sysset.closetext
                    });
                    s.setCache("sysset", t.data.sysset);
                }
                o(t.data);
            }
        }), l.fail = function(t) {
            i && d.hideLoading(), d.alert(t.errMsg);
        }, wx.request(l);
    },
    post: function(t, e, n, o, i) {
        this.json(t, e, n, o, !0, i);
    },
    get: function(t, e, n, o, i) {
        this.json(t, e, n, o, !1, i);
    },
    getDistanceByLnglat: function(t, e, n, o) {
        function i(t) {
            return t * Math.PI / 180;
        }
        var a = i(e), r = i(o), s = a - r, c = i(t) - i(n), u = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(s / 2), 2) + Math.cos(a) * Math.cos(r) * Math.pow(Math.sin(c / 2), 2)));
        return u *= 6378137, u = Math.round(1e4 * u) / 1e7;
    },
    alert: function(e, n) {
        "object" === (0, t.default)(e) && (e = JSON.stringify(e)), wx.showModal({
            title: "提示",
            content: e,
            showCancel: !1,
            success: function(t) {
                t.confirm && "function" == typeof confirm && n();
            }
        });
    },
    confirm: function(e, n, o) {
        "object" === (0, t.default)(e) && (e = JSON.stringify(e)), wx.showModal({
            title: "提示",
            content: e,
            showCancel: !0,
            success: function(t) {
                t.confirm ? "function" == typeof n && n() : "function" == typeof o && o();
            }
        });
    },
    loading: function(t) {
        void 0 !== t && "" != t || (t = "加载中"), wx.showToast({
            title: t,
            icon: "loading",
            duration: 5e6
        });
    },
    hideLoading: function() {
        wx.hideToast();
    },
    toast: function(t, e) {
        e || (e = "success"), wx.showToast({
            title: t,
            icon: e,
            duration: 1e3
        });
    },
    success: function(t) {
        wx.showToast({
            title: t,
            icon: "success",
            duration: 1e3
        });
    },
    upload: function(t) {
        var e = this;
        wx.chooseImage({
            success: function(n) {
                e.loading("正在上传...");
                var o = e.getUrl("util/uploader/upload", {
                    file: "file"
                }), i = n.tempFilePaths;
                wx.uploadFile({
                    url: o,
                    filePath: i[0],
                    name: "file",
                    success: function(n) {
                        e.hideLoading();
                        var o = JSON.parse(n.data);
                        if (0 != o.error) e.alert("上传失败"); else if ("function" == typeof t) {
                            var i = o.files[0];
                            t(i);
                        }
                    }
                });
            }
        });
    },
    pdata: function(t) {
        return t.currentTarget.dataset;
    },
    data: function(t) {
        return t.target.dataset;
    },
    phone: function(t) {
        var e = this.pdata(t).phone;
        wx.makePhoneCall({
            phoneNumber: e
        });
    },
    pay: function(e, n, o) {
        return "object" == (0, t.default)(e) && ("function" == typeof n && (e.success = n, 
        "function" == typeof o && (e.fail = o), void wx.requestPayment(e)));
    },
    cartcount: function(t) {
        this.get("member/cart/count", {}, function(e) {
            t.setData({
                cartcount: e.cartcount
            });
        });
    },
    onShareAppMessage: function(t, e) {
        var n = getApp(), o = n.getCache("sysset"), i = o.share || {}, a = n.getCache("userinfo_id"), r = o.shopname || "", s = o.description || "";
        return i.title && (r = i.title), e && (r = e), i.desc && (s = i.desc), {
            title: r,
            desc: s,
            path: (t = -1 != (t = t || "/pages/index/index").indexOf("?") ? t + "&" : t + "?") + "mid=" + a
        };
    },
    str2Obj: function(t) {
        if ("string" != typeof t) return t;
        if (t.indexOf("&") < 0 && t.indexOf("=") < 0) return {};
        var n = t.split("&"), o = {};
        return e.each(n, function(t, e) {
            if (e.indexOf("=") > -1) {
                var n = e.split("=");
                o[n[0]] = n[1];
            }
        }), o;
    },
    countDown: function(t, e) {
        var n = parseInt(Date.now() / 1e3), o = 0;
        if (t && (o = t > n ? t - n : n - t, o = parseInt(o)), e && (o = parseInt(e)), 0 == o) return !1;
        var i = Math.floor(o / 86400), a = Math.floor((o - 24 * i * 60 * 60) / 3600), r = Math.floor((o - 24 * i * 60 * 60 - 3600 * a) / 60), s = Math.floor(o - 24 * i * 60 * 60 - 3600 * a - 60 * r);
        return [ i, a < 10 ? "0" + a : a, r < 10 ? "0" + r : r, s < 10 ? "0" + s : s ];
    },

    
    //省级地址码校验
    checkProv: function (val) {
        var pattern = /^[1-9][0-9]/;
        var provs = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门" };
        if (pattern.test(val)) {
        if (provs[val]) {
            return true;
        }
        }
        return false;
    },
    //出生日期码校验
    checkDate: function (val) {
        var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
        if (pattern.test(val)) {
        var year = val.substring(0, 4);
        var month = val.substring(4, 6);
        var date = val.substring(6, 8);
        var date2 = new Date(year + "-" + month + "-" + date);
        if (date2 && date2.getMonth() == (parseInt(month) - 1)) {
            return true;
        }
        }
        return false;
    },
    //校验码校验
    checkCode : function (val) {
        var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
        var code = val.substring(17);
        if (p.test(val)) {
        var sum = 0;
        for (var i = 0; i < 17; i++) {
            sum += val[i] * factor[i];
        }
      
        if (parity[sum % 11] == code.toUpperCase()) {
            return true;
        }
        }
        
        return false;
    },

    //身份证规则校验
    checkIDCard : function (val) {
        if(this.checkCode(val)) {
            var date = val.substring(6,14);
            if(this.checkDate(date)) {
                if(this.checkProv(val.substring(0,2))) {
                    return true;
                }
            }
        }
        return false;
    },

    //身份证规则校验
    checkPhone : function (val) {
        if ((/^1[3456789]\d{9}$/.test(val))) {
            return true;
        }else{
            return false;
        }
        
    }
};